<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/11/19
 * Time: 15:11
 */

namespace Api\Behavior;



use Api\Controller\MessageController;
use Api\Service\AliBaichuanService;
use Api\Vendor\NetWarehourse\WHOrderNotify;
use Shop\Api\OrdersContactInfoApi;
use Shop\Api\OrdersExpressApi;
use Tool\Api\MessageApi;
use Tool\Api\MessageBoxApi;
use Tool\Model\MessageBoxModel;
use Tool\Model\MessageModel;

/**
 * 发送订单确认
 * Class OrderConfirmBehavior
 * @package Api\Behavior
 */
class OrderDeliverBehavior  extends \Think\Behavior{

    public function run(&$params){

        addLog("OrderDeliverBehavior",$params,"","[调试]调用订单确认短消息");
        $now_time = time();
        $order_code = $params['order_code'];

        $map = array(
            'order_code'=>$order_code,
        );


        $result = apiCall(OrdersContactInfoApi::GET_INFO,array($map));

        if(!$result['status']){
            LogRecord($result['info'],__FILE__.__LINE__);
            return;
        }
        if(!is_array($result['info'])) {
            LogRecord("订单编号".$order_code."无效",__FILE__.__LINE__);
            return;
        }
        $mobile = $result['info']['mobile'];

        //自动推送订单到网仓
        $this->autoPushOrderToNetWH($order_code);

        $result = apiCall(OrdersExpressApi::GET_INFO,array($map));

        if(!$result['status']){
            LogRecord($result['info'],__FILE__.__LINE__);
            return;
        }
        if(!is_array($result['info'])) {
            LogRecord("订单编号".$order_code."无效",__FILE__.__LINE__);
            return;
        }

        $uid = $result['info']['uid'];
        $express_no = $result['info']['expressno'];
        $express_code = $result['info']['expresscode'];
        if(empty($express_no)){

            //物流单号是空的
            addLog("OrderDeliverBehavior",$express_no,"","[调试] 物流单号是空的 调用订单确认短消息");
            return ;
        }
        $allExpress = C('CFG_EXPRESS');
        $express_company = $allExpress[$express_code];
        $title = "[订单消息]";
        $summary = "订单:".$order_code.",已发货,发货公司: ". $express_company .",快递单号: ".$express_no;
        $content =  "订单:".$order_code.",已发货,发货公司: ". $express_company .",快递单号: ".$express_no;
        $dtree_type =C("DATATREE.MSG_TYPE_ORDER");
        if(empty($dtree_type)){
            $dtree_type = "";
        }
        $entity = array(
            'title'=>$title,
            'content'=>$content,
            'summary'=>$summary,
            'dtree_type'=> $dtree_type, //订单消息
            'create_time'=>$now_time,
            'send_time'=>$now_time,
            'from_id'=>-1,
            'status'=> 1, //数据状态
            'to_id'=>$uid,
        );

        $result = apiCall(MessageApi::ADD,array($entity));

        if(!$result['status']){
            LogRecord($result['info'],__FILE__.__LINE__);
            return;
        }
        if(empty($result['info'])) {
            LogRecord("订单编号".$order_code."无效",__FILE__.__LINE__);
            return;
        }
        $msg_id =  $result['info'];
        $messageBoxEntity = array(
            'to_id'=>$uid,
            'msg_status'=>MessageBoxModel::STATUS_UNREAD,
            'msg_id'=>$msg_id,
        );

        $result = apiCall(MessageBoxApi::ADD,array($messageBoxEntity));

        if(!$result['status']){
            LogRecord($result['info'],__FILE__.__LINE__);
            return;
        }



        //如果开启了阿里百川消息服务
        $service = new AliBaichuanService();

        if(strlen($mobile) == 11){


            $result = $service->deliverOrderMsg($mobile,$order_code,$express_company,$express_no);
            if($result['status']){
                LogRecord($result['info'],__FILE__.__LINE__);
            }
            addLog("OrderDeliverBehavior",$params,$result,"[调试]调用订单发货短消息返回结果");

        }else{
            LogRecord("用户手机号".$mobile."不合法!",__FILE__.__LINE__,'INFO');
        }

    }

    /**
     * 推送订单到网仓
     * @param $order_code
     */
    private function autoPushOrderToNetWH($order_code){

        $request = new WHOrderNotify();

        $request->request($order_code);

    }

}